package org.scalacn.easyscala.actor


import net.liftweb._
import http._
import actor._
import util._
import common._
import mapper._

object MessageActor {
  import org.scalacn.easyscala.model.Message
  val ma = new MessageActor()
  def apply() = ma
  
  case class UserListen(who: LiftActor)
  case class UnUserlisten(who: LiftActor)
  case class AddMessage(msg: Message)
  case class MessageReceived(msg: Message)
 
}

class MessageActor extends LiftActor {

  private var listeners: List[LiftActor] = Nil
  
  protected def messageHandler = {
    case MessageActor.UserListen(who) => listeners = who :: listeners 
    case MessageActor.UnUserlisten(who) => listeners = listeners.filter(_ ne who) 
    case MessageActor.AddMessage(msg) => {
    								msg.save()
    								listeners.foreach(v => v ! MessageActor.MessageReceived(msg))
    								}
    
  }
  
  
}
